#!/bin/sh

# Debian (Ubuntu) packages tested (plus databases where available):
# * espeak, festival, flite, libttspico-utils (svox)
# * mbrola
# * festival with experimental unit selection databases from
#  http://festvox.org/cmu_arctic/index.html
#
# TODO:
# * htsengine (samples sound similar to flite)
# * http://mary.dfki.de/Download
#
# A nice feature of the implemented workflow is that all
# tools are forced to waveform file output.
# This is especially handy for future usage on portable devices.
#
# Related links:
# http://blog.efrontlearning.net/2011/01/free-and-open-source-text-to-speech-tools-for-elearning.html
# http://www.webbie.org.uk/Veli-Pekka/reviews_of_speech_synths.html

main() {
 T=$PWD/tmp.txt
 T2=$PWD/tmp2.txt
 USEBEST="" # non-null if execute only the better sounding tests
 KEEPOUT="1" # non-null if keep output files instead of playing them
 FESTVOX_DIR=$HOME/home/tts/festvox_voices
 en
 hu
}

hu() {
 SLANG="text_hu"

 tts s_espeak_slow "hungarian"

 tts s_espeak_mbrola_slow "hu1"
}

en() {
 SLANG="text_en"

 tts s_libttspico "en-US en-GB" "en-US en-GB"

 tts s_espeak "default en-scottish english lancashire english_rp english_wmids english-us en-westindies" ""

 tts s_espeak \
  "`prefix english_rp+ whisperf f1 f2 f3 f4 f5 klatt klatt2 klatt3 m1 m2 m3 m4 m5 m6 m7 whisper croak`" \
  "`prefix english_rp+ f2 f4 klatt klatt3 m7`"

#TODO us1/us1mrpa
 tts s_espeak_mbrola "en1 us1 us2 us3" "us1 us2"

 tts s_flite "kal16 awb rms slt" "awb rms slt"

 tts s_festival_diphone "rab ked don kal" "rab kal"

 tts s_festival_clunits \
  "cmu_us_awb_arctic cmu_us_bdl_arctic cmu_us_jmk_arctic cmu_us_ksp_arctic cmu_us_rms_arctic cmu_us_slt_arctic cmu_us_clb_arctic" \
  "cmu_us_jmk_arctic cmu_us_bdl_arctic cmu_us_slt_arctic cmu_us_clb_arctic"
}

tts() {
 local SYNTH="$1"
 local ALL="$2"
 local GOOD="$3"
 [ -z "$GOOD" ] && GOOD="$ALL"

 if [ -n "$USEBEST" ] ; then
  local SET="$GOOD"
 else
  local SET="$ALL"
 fi

 for VOICE in $SET; do
  local B="${SYNTH}_$VOICE"
  {
   $SLANG
   if [ "$SLANG" = "text_hu" ] ; then
    echo "Felolvasta: $SYNTH ${VOICE}."
   else
    echo "Read aloud by $SYNTH ${VOICE}."
   fi |
   sed "s~\<s_~~ ; s~_~ ~g"
  } > $T
  O=$PWD/$B.wav
  echo "now generating: $B"
  $SYNTH
  if [ -z "$KEEPOUT" ] ; then
   aplay $O
   rm -v $O
  fi
 done
}

s_festival_clunits() {
  local V=${VOICE}_clunits
  local P="$PWD"
  cd ${FESTVOX_DIR}/$VOICE || exit 1

#  echo '(voice_'${VOICE}')(tts "'$T'" nil)' |
#  festival --pipe festvox/${VOICE}.scm

  PORT=1314
  echo "(set! server_port ${PORT})" > "$T2"
  festival --server "$T2" festvox/${V}.scm &
  PID=$!
  cd "$P"
  while ! nc -zw1 localhost $PORT ; do
   kill -0 $PID || exit 1
   printf "."
   sleep 0.1
  done
  echo '(voice_'${V}')' > "$T2"
  festival_client --port $PORT --prolog "$T2" --ttw "$T" --output "$O"
  kill $PID
  while kill -0 $PID 2>/dev/null
  do
   printf "."
   sleep 0.1
  done
}

s_libttspico() {
 pico2wave -w $O -l "$VOICE" "`cat $T`"
}

s_festival_diphone() {
 text2wave -eval "(voice_${VOICE}_diphone)" -o $O $T
}

s_flite() {
 flite -voice $VOICE -f $T -o $O
}

s_espeak() {
 espeak -z -v $VOICE -f $T -w $O
}

s_espeak_slow() {
 espeak -z -g 3 -s 150 -p 10 -v $VOICE -f $T -w $O
}

s_espeak_mbrola() {
 espeak -z -v mb-$VOICE -f $T -q --pho |
 mbrola /usr/share/mbrola/$VOICE/$VOICE - $O
}

s_espeak_mbrola_slow() {
 espeak -z -g 3 -s 150 -p 50 -v mb-$VOICE -f $T -q --pho |
 mbrola -e /usr/share/mbrola/$VOICE/$VOICE - $O
}

prefix() {
 PRE="$1"
 shift 1
 echo "$@" |
 sed "s~\<~${PRE}~g"
}

ugly() {
# couldn't install gnuspeech in reasonable time, so skipped that

 freetts
# Performance probably similar to flite, but couldn't test as
# this is the only documentation that was included:
#
#main
#
#public static void main(java.lang.String[] args)
#The main entry point for FreeTTS.

 cicero # OSS sound

 recite # OSS sound
}

text_en() {
 cat << EOF
Speech synthesis is the artificial production of human speech.
EOF
}

text_en7() {
 cat << EOF
Not at this particular case, Tom, apologized Whittemore. For the twentieth time that evening the two men shook hands.
EOF
}

text_en6() {
 cat << EOF
The Earth, our world, is the third planet from the Sun. It is one of the four terrestrial planets in our Solar System. This means most of its mass is solid. The other three are Mercury, Venus and Mars.

The Earth is home to millions of species of plants and animals, including humans. Earth is the only place in the universe where life has been confirmed to exist. The Earth formed around 4.5 billion years ago. The things that live on Earth have had a great impact on many aspects of the planet. For example, early lifeforms completely changed its air (atmosphere). The total of all life is called the biosphere.
EOF
}

text_en5() {
 cat << EOF
What? Harmless? Is that all it's got to say? Harmless! One
word!

Ford shrugged.

Well, there are a hundred billion stars in the Galaxy, and only
a limited amount of space in the book's microprocessors, he
said, and no one knew much about the Earth of course.

Well for God's sake I hope you managed to rectify that a bit.

Oh yes, well I managed to transmit a new entry off to the
editor. He had to trim it a bit, but it's still an improvement.

And what does it say now? asked Arthur.

Mostly harmless, admitted Ford with a slightly embarrassed
cough.
EOF
}

text_en4() {
 cat << EOF
As a Learning consultant I would prefer to use a human narration professional. However, you should consider the following factors:

It is more expensive.
It needs more time.
It is difficult to maintain the training.
Few professional have the necessary skills.
It is difficult to update the Learning course.
The pace is not steady.
It is difficult to maintain your organizations’ identity.
EOF
}

text_en3() {
 cat << EOF
The Chaos

A poem on English pronunciation, by:
Charivarius, (G.N. Trenite: 1870—1946).

Dearest creature in creation,
Studying English pronunciation,
I will teach you in my verse
Sounds like corpse, corps, horse and worse.
It will keep you, Susy, busy,
Make your head with heat grow dizzy;
Tear in eye your dress you’ll tear.
So shall I! Oh hear my prayer:
Pray console your loving poet,
Make my coat look new, dear, sew it.
Just compare heart, beard and heard,
Dies and diet, Lord and word.
Sword and sward, retain and Britain,
(Mind the latter, how it’s written).
Made has not the sound of bade,
Say—said, pay—paid, laid, but plaid.
Now I surely will not plague you
With such words as vague and ague,
But be careful how you speak:
Say break, steak, but bleak and streak,
Previous, precious; fuschia, via;
Pipe, shipe, recipe and choir;
Cloven, oven; how and low;
Script, receipt; shoe, poem, toe.
Hear me say, devoid of trickery;
Daughter, laughter and Terpsichore;
Typhoid, measles, topsails, aisles;
Exiles, similes, reviles;
Wholly, holly; signal, signing;
Thames, examining, combining;
Scholar, vicar and cigar,
Solar, mica, war, and far.
Desire—desirable, admirable—admire;
Lumber, plumber; bier but brier;
Chatham, brougham; renown but known,
Knowledge; done, but gone and tone,
One, anemone; Balmoral,
Kitchen, lichen; laundry, laurel;
Gertrude, German; wind and mind;
Scene, Melpomene, mankind;
Tortoise, turquoise, chamois-leather.
This phonetic labyrinth
Gives moss, gross; brook, brooch; ninth, plinth.
Billet does not end like ballet;
Bouquet, wallet, mallet, chalet;
Blood and flood are not like food,
Nor is mould like should and would.
Banquet is not nearly parquet,
Which is said to rime with darky.
Viscous, viscount; load and broad;
Toward, to forward, to reward.
And your pronunciation’s O.K.
When you say correctly; croquet;
Rounded, wounded; grieve and sieve;
Friend and fiend, alive and live,
Liberty, library; heave and heaven;
Rachel, ache, moustache; eleven.
We say hallowed, but allowed;
People, leopard; towed, but vowed.
Mark the difference moreover
Between mover, plover, Dover;
Leeches, breeches; wise, precise;
Chalice, but police and lice.
Camel, constable, unstable,
Principle, discipline, label;
Petal, penal and canal;
Wait, surmise, plait, promise; pal.
Suit, suite, ruin; circuit, conduit,
Rime with: “shirk it” and “beyond it”;
But it is not hard to tell
Why it’s pall, mall, but PallMall.
Muscle, muscular; goal and iron;
Timber, climber; bullion and lion;
Worm and storm; chaise, chaos, chair;
Senator, spectator, mayor.
Ivy, privy; famous; clamour,
And enamour rime with “hammer”.
Pussy, hussy and possess,
Desert, but dessert, address.
Golf, wolf; countenants; lieutenants
Hoist, in lieu of flags, left pennants.
River, rival; tomb, bomb, comb;
Doll and roll, and some and home.
Stranger does not rime with anger,
Neither does devour with clangour.
Soul, but foul; and gaunt, but aunt;
Font, front, won’t; want, grand and grant;
Shoes, goes, does. Now first say finger,
And then; singer, ginger, linger.
Real, zeal; mauve, gauze and gauge;
Marriage, foliage, mirage, age.
Query does not rime with very,
Nor does fury sound like bury.
Dost, lost, post; and doth, cloth, loth;
Job, Job; blossom, bosom, oath.
Though the difference seems little
We say actual, but victual;
Seat, sweat; chaste, caste; Leigh, eight, height;
Put, nut; granite but unite.
Reefer does not rime with deafer,
Feoffer does, and zephyr, heifer.
Dull, bull; Geoffrey, George; ate, late;
Hint, pint; senate, but sedate.
Scenic, Arabic, Pacific;
Science, conscience, scientific.
Tour, but our, and succour, four;
Gas, alas and Arkansas!
Sea, idea, guinea, area,
Psalm, Maria, but malaria.
Youth, south, southern; cleanse and clean;
Doctrine, turpentine, marine.
Compare alien with Italian.
Dandelion with battalion,
Sally with ally, Yea, Ye,
Eye, I, ay, aye, whey, key, quay.
Say aver, but ever, fever,
Neither, leisure, skein, receiver.
Never guess—it is not safe;
We say calves, valves; half, but Ralf.
Heron, granary, canary;
Crevice and device and eyrie;
Face, preface, but efface,
Phlegm, phlegmatic; ass, glass, bass;
Large, but target, gin, give, verging;
Ought, out, joust and scour, but scourging;
Ear, but earn; and wear and tear
Do not rime with “here” but “ere”.
Seven is right, but so is even;
Hyphen, roughen, nephew, Stephen;
Monkey, donkey; clerk and jerk;
Asp, grasp, wasp; and cork and work.
Pronunciation—think of psyche—
Is a paling, stout and spikey;
Won’t it make you lose your wits,
writing groats and saying “groats”?
It’s a dark abyss or tunnel,
Strewn with stones, like rowlock, gunwale,
Islington and Isle of Wight,
Housewife, verdict and indict.
Don’t you think so, reader, rather
Saying lather, bather, father?
Finally: which rimes with “enough”,
Though, through, plough, cough, hough or tough?
Hiccough has the sound of “cup”,
My advice is ... give it up!
EOF
}

text_hu() {
 cat << EOF
A beszédszintézis célja az, hogy mesterségesen hozzon létre beszélő emberi hangot.
EOF
}

text_hu2() {
 cat << EOF
Fegyverben réved fönn a téli ég, 
kemény a menny és vándor a vidék, 
halkul a hó, megáll az elmenő, 
lehelete a lobbant keszkenő. 

Hol is vagyok? Egy szalmaszál nagyon 
helyezkedik a csontozott úton; 
kis, száraz nemzet; izgágán szuszog, 
zúzódik, zizzen, izzad és buzog. 

De fönn a hegyen ágyat bont a köd, 
mint egykor melléd: mellé leülök. 
Bajos szél jaját csendben hallgatom, 
csak hulló hajam repes vállamon. 

Óh szív! nyugodj! Vad boróka hegyén 
szerelem szólal, incseleg felém, 
pirkadó madár, karcsú, koronás, 
de áttetsző, mint minden látomás.

- József Attila, Óh szív! Nyugodj! (1928)
EOF
}

main "$@"

